package com.yoo.module_base.view

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import com.yoo.module_base.R
import com.yoo.module_base.utils.setOnClickCallback

class TitleBar : ConstraintLayout {

    private var mContext: Context? = null
    private var viewBack:ImageView?=null
    private var viewMore:ImageView?=null
    private var txtTitle: TextView?=null
    private var titleBarContainer: ConstraintLayout?=null

    constructor(context: Context) : this(context = context, null)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        this.mContext = context
        initView(attrs)
    }

    @SuppressLint("MissingInflatedId", "CutPasteId")
    private fun initView(attrs: AttributeSet?) {
       val view = LayoutInflater.from(mContext).inflate(R.layout.view_title_bar,this)
        titleBarContainer = view.findViewById(R.id.title_bar_container)
        viewBack = view.findViewById(R.id.viewBack)
        viewMore = view.findViewById(R.id.viewMore)
        txtTitle = view.findViewById(R.id.txtTitle)
    }

    fun setTitleBarBackground(resId:Int){
        titleBarContainer?.setBackgroundResource(resId)
    }
    fun setTitleBarBackgroundColor(colorId:Int){
        titleBarContainer?.setBackgroundColor(colorId)
    }
    fun setTitleBarPadding(top:Int){
        titleBarContainer?.setPadding(0,top,0,0)
    }

    fun setTitleText(text:String){
        txtTitle?.text = text
        txtTitle?.isVisible = true
    }
    fun setTitleVisible(isVisible:Boolean){
        txtTitle?.isVisible = isVisible
    }
    fun setTitleColor(colorRes:Int){
        txtTitle?.setTextColor(colorRes)
    }
    fun setBackListener(callBack:()->Unit){
        viewBack?.setOnClickCallback{
            callBack.invoke()
        }
    }
    fun setTitleCLickListener(callBack:()->Unit){
        txtTitle?.setOnClickCallback{
            callBack.invoke()
        }
    }
    fun setMoreListener(callBack:()->Unit){
        viewMore?.setOnClickCallback{
            callBack.invoke()
        }
    }
    fun setMoreVisible(isVisible:Boolean){
        viewMore?.isVisible = isVisible
    }
    fun setMoreResId(resId:Int){
        viewMore?.setImageResource(resId)
    }
}